const mongoose = require('../db');

const pageSchema = new mongoose.Schema({
  key: { type: String, default: 'default', unique: true },
  data: { type: mongoose.Schema.Types.Mixed, default: { canvas: [] } }
});

const Page = mongoose.model('Page', pageSchema);

exports.readPageData = async () => {
  try {
    const doc = await Page.findOne({ key: 'default' });
    if (!doc) return { canvas: [] };
    return doc.data;
  } catch (err) {
    return { canvas: [] };
  }
};

exports.writePageData = async (data) => {
  try {
    await Page.findOneAndUpdate(
      { key: 'default' },
      { data },
      { upsert: true, new: true }
    );
    return null;
  } catch (err) {
    return err;
  }
}; 